from .question_utils import *


def score_submit_homework(homework_id, user_id):
    homework = Homework.query.filter_by(homework_id=homework_id).limit(1).first()
    questions = json.loads(homework.homework_question)
    total_score = 0

    for question in questions:
        question_id = question["question"]
        all_score = question["score"]
        user_submits = UserSubmit.query.filter_by(question_id=question_id, user_id=user_id).all()

        for user_submit in user_submits:
            if user_submit.create_time < homework.start_time or user_submit.create_time > homework.end_time:
                continue
            result = user_submit.result
            result_info = json.loads(user_submit.result_info)
            question_id = user_submit.question_id
            get_keyword_number = []
            score = 0

            # 获得问题关键字
            real_question, question_type = get_real_question(question_id)
            keyword = json.loads(real_question.keyword)

            # 全对
            if result == 1:
                score = all_score
            elif result == 2:
                # 填空题
                if question_type == 1:
                    num = 0
                    for get_keyword in result_info["userSubmitNormalFillResultInfoDTOList"]:
                        if get_keyword["correctOrWrong"] is True:
                            num += 1
                    score = num * all_score  # 填空题为每空多少分
                # 程序填空题
                if question_type == 4 or question_type == 5:
                    for get_keyword in result_info["userSubmitProgramFillKeywordResultDTOList"]:
                        for is_own in get_keyword["userSubmitProgramKeywordResultDTOList"]:
                            if is_own["own"] is True:
                                get_keyword_number.append((get_keyword["blankNumber"], is_own["keywordNumber"]))
                    for answer in keyword:
                        for keyword_number in get_keyword_number:
                            if answer["blankNumber"] == keyword_number[0]:
                                for single_score in answer["keywordList"]:
                                    if single_score["keywordNumber"] == keyword_number[1]:
                                        score += single_score["score"]
                # 编程题
                if question_type == 6:
                    for get_keyword in result_info["userSubmitProgramKeywordResultDTOList"]:
                        if get_keyword["own"] is True:
                            get_keyword_number.append(get_keyword["keywordNumber"])
                    for point in keyword:
                        for number in get_keyword_number:
                            if point["keywordNumber"] == number:
                                score += point["score"]
            max_score = max(max_score, score)
        total_score += max_score

    res = StudentHomework.query.filter_by(student_id=user_id, homework_id=homework_id).update({"score": total_score})
    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise APIException(UPDATE_ERROR)
    if res == 1:
        return total_score
    else:
        raise APIException(UPDATE_ERROR)
